﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_ApplicationsHolder_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);
        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);
        dlPageChildren.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlPageChildren_ItemDataBound);
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);

        dsTabs.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsTabs_SelectData);


        dsParent.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsParent_SelectData);
        dlNavParent.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavParent_ItemDataBound);

    }
    void dsTabs_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        var tabs = new List<Post>();
        foreach (var child in AcmsContext.CurrentPost.Children)
        {
            tabs.AddRange(child.Children.Where(x => x.TemplateName == "TabHolder-NodeParent").ToList());
        }
        e.SetData(tabs);
    }

    void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var pnlItem = e.Item.FindControl("pnlItem") as Panel;
            ///
            if (pnlItem != null)
            {
                //pnlItem.BackImageUrl = "~/Images/dbImages/systems-1.jpg";
                //var control = acmsPage.AcmsContext.CurrentPost.FindPostControl("Backgound Image");

                var HeroImage = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
                if (HeroImage != null)
                {
                    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlItem.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            }

            if (pnlItem != null && e.DataItem == dlFeed.Items[0].DataItem)
            {
                pnlItem.CssClass += "active";
            }
            ///

            


        }
    }

    void dlPageChildren_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNavChildren") as HyperLink;

            //lnk.Text = "hello";// AcmsContext.CurrentPost.DisplayName;
            
            if (lnk != null)
            {
                lnk.Text = post.DisplayName;    

            }


            //else
            //{
            //    pnlItem.Style.Add("display", "none");
            //}

        }
    }

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNavSibling") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }


    void dsParent_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dlNavParent_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}
